/*
 * Tree List Plug-in
 * 
 * Copyright (c) 2009 Arthur McLean (arthur@mclean.ws)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
 */

; // In case a previously included plug-in doens't end with a semicolon.
(function($) {
	$.fn.tree_list = function() {
		
		this.each(function() {
			// element-specific code here
			$(this).find("img.tree_icon").click(function() {
				var img_src = $(this).attr("src");
				if(img_src === "/images/admin/icons/bullet_toggle_plus.png")
				{
					// They clicked to expand the tree
					show_div($(this).attr("id"));
					
					// The next action will be to collapse, so change the image
					$(this).attr("src", "/images/admin/icons/bullet_toggle_minus.png");
					$(this).attr("alt", "Collapse Tree");
				}
				else
				{
					// They clicked to collapse the tree
					hide_div($(this).attr("id"));
					
					// The next action will be to collapse, so change the image
					$(this).attr("src", "/images/admin/icons/bullet_toggle_plus.png");
					$(this).attr("alt", "Expand Tree");
				}
				
			});
		});
		
		
		function show_div(id) {
			$("div[class='" + id + "']").show("slow").find("img.tree_icon").each(function() {
				if($(this).attr("src") === "/images/admin/icons/bullet_toggle_minus.png")
				{
					// Found an image with an expanded div
					show_div($(this).attr("id"));
				}
			});
		} // end show_div
		
		function hide_div(id) {
			$("div[class='" + id + "']").hide("slow").find("img.tree_icon").each(function() {
				hide_div($(this).attr("id"));
			});
			
		} // end hide_div
		
		return this;
	};
})(jQuery);
